<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News_model extends CI_Model{

	  function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

	public function cateshow($cid)
	{
		$this->load->database();
		$this->db->order_by("catename", "desc");
		if($cid == 0)
		{
			//$query = $this->db->get_where('cate',array('COALESCE(cateid2,0)'=>0));
			$QueryString="SELECT * FROM tblcate WHERE COALESCE(cateid2,0) = 0 limit 7";
			$query = $this->db->query($QueryString);
		}else{
			$query = $this->db->get_where('tblcate',array('cateid1'=>$cid));
		}
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	
	public function catehide($cid)
	{
		$this->load->database();
		$this->db->order_by("catename", "desc");
		if($cid == 0)
		{
			//$query = $this->db->get_where('cate',array('COALESCE(cateid2,0)'=>0));
			$QueryString="SELECT * FROM tblcate WHERE COALESCE(cateid2,0) = 0 limit 7,100";
			$query = $this->db->query($QueryString);
		}else{
			$query = $this->db->get_where('cate',array('cateid1'=>$cid));
		}
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	
	public function banner()
	{
		$this->load->database();
		
		$QueryString="SELECT * FROM tblbanner WHERE active =1 and isbanner=1 order by createdate desc";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}

	public function newhot()
	{
		$this->load->database();
		
		$QueryString="SELECT * FROM tblbanner WHERE active =1 and isbanner=0 order by createdate desc";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	public function shownews($cid)
	{
		$this->load->database();
		$this->db->order_by("contentid", "desc");
		if($cid == 0)
		{
			$query = $this->db->get('tblcontent');
		}else{
			$query = $this->db->get_where('tblcontent',array('cateid'=>$cid));
		}
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } else {
          return array();
        }
	}
	
	public function product($cateid,$content,$begin,$end,$orderby)
	{
		$this->load->database();
		$search="";
		
		if(	strlen($orderby)>0)
		{
			$orderby = " ORDER BY " . $orderby;
		}
		else
		{
			$orderby = " order by createdate desc ";
		}
		if(	strlen($content)>0)
		{
			$search = $search . " AND (catename like '%" . $content . "%' OR productname like '%" . $content . "%' OR content like '%" . $content . "%')";
		}
		if($cateid>0)
		{
			$listcate = $this->ListCate($cateid);
			$QueryString="SELECT * FROM tblproduct WHERE active =1 and cateid in (" . $listcate . ")" . $search . $orderby . " limit " . $begin . "," . $end;
			//$QueryString="SELECT * FROM tblproduct WHERE active =1 and cateid=".$cateid." " . $search . $orderby . " limit " . $begin . "," . $end;
			//echo $QueryString;
		}
		else
		{
			$QueryString="SELECT * FROM tblproduct WHERE active =1 " . $search . $orderby . " limit " . $begin . "," . $end;
		}
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result() ;
        }
		return array();
	}
	
	public function producthot()
	{
		$this->load->database();
		$search="";
		$QueryString="SELECT * FROM tblproduct WHERE active =1 and ishot = 1 order by createdate desc limit 5";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result() ;
        }
		return array();
	}
	
	public function ListCate($cateid){
		if($cateid<1)
		{
			return "0";
		}
		$this->load->database();
		$listcate="0";
		$QueryString="SELECT * FROM tblcate WHERE active =1 AND COALESCE(cateid2,0)>0 AND (cateid1 in (SELECT cateid1 FROM tblcate WHERE active =1 AND cateid = ".$cateid." AND COALESCE(cateid2,0)=0)OR cateid = ".$cateid.") order by catename desc";
		//echo $QueryString;
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
		  {
		  	$listcate = $listcate . ",". $row->cateid;
		  }
        }
		return $listcate;
	}
	public function ListCateHome(){
		$this->load->database();
		$QueryString="SELECT * FROM tblcate WHERE active =1 and ishome = 1 order by catename desc";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	public function roomhot($ishot)
	{
		$this->load->database();
		if($ishot==1){
			$QueryString="SELECT * FROM tblroom WHERE active =1 order by ishot desc,id desc";			
		}
		else {
			$QueryString="SELECT * FROM tblroom WHERE active =1 order by id desc";
		}
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function listcateid($cateid)
	{
		$this->load->database();
		$QueryString="SELECT * FROM tblcate WHERE active =1 AND COALESCE(cateid2,0)>0 AND cateid1 in (SELECT cateid1 FROM tblcate WHERE active =1 AND cateid = ".$cateid.")";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function getcatename($cateid)
	{
		$this->load->database();
		$QueryString="SELECT * FROM tblcate WHERE active =1 AND cateid = ".$cateid;
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
          {
          	return $row->catename;
          }
        }
		return "";
	}
	
	function getparentcatename($cateid)
	{
		$this->load->database();
		$QueryString="SELECT * FROM tblcate WHERE active =1 AND COALESCE(cateid2,0)=0 AND cateid1 in (SELECT cateid1 FROM tblcate WHERE active =1 AND cateid = ".$cateid.")";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
          {
          	if($cateid!=$row){
          		return $row->catename;
          	}
          	return "";
          }
        }
		return "";
	}
	function getparentcateid($cateid)
	{
		$this->load->database();
		$QueryString="SELECT * FROM tblcate WHERE active =1 AND COALESCE(cateid2,0)=0 AND cateid1 in (SELECT cateid1 FROM tblcate WHERE active =1 AND cateid = ".$cateid.")";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
          {
          	return $row->cateid;
          }
        }
		return "";
	}
	
	function getproductcate($cateid)
	{
		$this->load->database();
		$QueryString="select tblcate.cateid,tblcate.catename,count(productid) as numberproduct from tblcate left join tblproduct on tblproduct.cateid=tblcate.cateid where COALESCE(tblcate.cateid2,0)>0 AND tblcate.cateid1 in (select cateid1 from tblcate where cateid=".$cateid.")group by tblcate.cateid,tblcate.catename;";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function getranking(){
		$this->load->database();
		$QueryString="select * from tblproduct  order by createdate desc,productid desc limit 6";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function gettotalproduct($cateid)
	{
		$this->load->database();
		$listcate = $this->ListCate($cateid);
		$QueryString="select count(1) as totalrow from tblproduct where cateid in(".$listcate.")";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
          {
          	return $row->totalrow;
          }
        }
		return 0;
	}
	
	function getproduct($cateid,$index)
	{
		$this->load->database();
		$listcate = $this->ListCate($cateid);
		$QueryString="select * from tblproduct where cateid in(".$listcate.") order by createdate desc,productid desc LIMIT ".(($index-1) * 40).",40";
		$query = $this->db->query($QueryString);
		//echo $QueryString;
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function getproducthide($cateid,$index)
	{
		$this->load->database();
		$listcate = $this->ListCate($cateid);
		$QueryString="select * from tblproduct where cateid in(".$listcate.") order by price,createdate desc,productid desc  LIMIT ".(($index-1) * 40).",40";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function getproductview($cateid)
	{
		$this->load->database();
		$listcate = $this->ListCate($cateid);
		$QueryString="select * from tblproduct where cateid in(".$listcate.") order by view desc,'like' desc,createdate desc,productid desc limit 100";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function getproductinfo($productid)
	{
		$this->load->database();
		$QueryString="UPDATE tblproduct SET view=COALESCE(view,0) +1,updatedate=now() WHERE productid=" .$productid;
		$query = $this->db->query($QueryString);
		$QueryString="select * from tblproduct where productid=" .$productid;
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function getproperties($productid)
	{
		
		$this->load->database();
		$QueryString="select * from tblproperties where productid=".$productid;
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        }
		return array();
	}
	
	function SearchProduc($content,$index)
	{
		$this->load->database();
		$QueryString="SELECT p.* FROM tblproduct p WHERE p.searchcontent like '%" . $content ."%' order by productid desc LIMIT ".(($index-1) * 20).",20";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	
	function SearchProducPrice($cateid,$price,$priceto,$index)
	{
		$this->load->database();
		$listcate = $this->ListCate($cateid);
		if($priceto>-1 && $price >-1)
		{
			$QueryString="SELECT p.* FROM tblproduct p WHERE (p.price between '".$price."' and '".$priceto."') and p.cateid in(".$listcate.") order by productid desc LIMIT ".(($index-1) * 20).",20";
		}
		else
		{
			$QueryString="SELECT p.* FROM tblproduct p WHERE p.cateid in(".$listcate.") order by productid desc LIMIT ".(($index-1) * 20).",20";
		}
		
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	function SearchProducPriceTotal($cateid,$price,$priceto)
	{
		$this->load->database();
		$listcate = $this->ListCate($cateid);
		if($priceto>-1 && $price >-1)
		{
			$QueryString="SELECT count(1) as totalrow FROM tblproduct p WHERE (p.price between '".$price."' and '".$priceto."') and p.cateid in(".$listcate.")";
		}
		else
		{
			$QueryString="SELECT count(1) as totalrow FROM tblproduct p WHERE p.cateid in(".$listcate.")";
		}
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
          {
          	return $row->totalrow;
          }
        }
		return 0;
	}
	function SearchProducTotal($content)
	{
		$this->load->database();
		$QueryString="SELECT count(1) as totalrow FROM tblproduct p WHERE p.searchcontent like '%" . $content ."%'";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
          {
          	return $row->totalrow;
          }
        }
		return 0;
	}
	
	function SearchRoom($content,$index)
	{
		$this->load->database();
		$QueryString="SELECT p.* FROM tblroom p WHERE p.searchcontent like '%" . $content ."%' order by id desc LIMIT ".(($index-1) * 20).",20";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	
	function SearchRoomTotal($content)
	{
		$this->load->database();
		$QueryString="SELECT count(1) as totalrow FROM tblroom p WHERE p.searchcontent like '%" . $content ."%'";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          foreach($query->result() as $row)
          {
          	return $row->totalrow;
          }
        }
		return 0;
	}
	
	function roominfo($id)
	{
		$this->load->database();
		$QueryString="SELECT p.* FROM tblroom p WHERE p.id = '" . $id ."'";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	function loadadventive($productid)
	{
		$this->load->database();
		$QueryString="SELECT * FROM((SELECT p.* FROM tblbanner p WHERE p.active=1 and isbanner=2 and productid = ".$productid." ORDER BY p.id DESC LIMIT 10) UNION (SELECT p.* FROM tblbanner p WHERE p.active=1  and isbanner=2 ORDER BY p.id DESC LIMIT 10)) AS tbl ORDER BY id DESC LIMIT 10 ";
		$query = $this->db->query($QueryString);
		if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
		else 
		{
          return array();
        }
	}
	
}
?>